package com.yhp.web;

import com.yhp.bean.Grade;
import com.yhp.bean.Student;
import com.yhp.service.impl.GradeServiceImpl;
import com.yhp.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.List;

@WebServlet(urlPatterns = "/Educational/student/Student")
public class StudentServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if("get".equals(method)){
            getStudent(req,resp);
        }else if("add".equals(method)){
            addStudent(req,resp);
        }else if("delete".equals(method)){
            deleteStudent(req,resp);
        }else if("update".equals(method)){
            updateStudent(req,resp);
        }else if("findById".equals(method)){
            findById(req,resp);
        }
    }

    //添加学生信息
    private void addStudent(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("utf-8");
        /**获取数据*/
        //姓名
        String stuName = req.getParameter("stuName");
        //学号
        String stuNo = req.getParameter("stuNo");
        //班级
        String gid = req.getParameter("gid");
        //性别
        String sex = req.getParameter("sex");
        //手机号
        String phone = req.getParameter("phone");
        //邮箱
        String email = req.getParameter("email");
        //户口所在地
        String registered = req.getParameter("registered");
        //住址
        String addRess = req.getParameter("addRess");
        //专业
        String profession = req.getParameter("profession");
        //政治面貌
        String politics = req.getParameter("politics");
        //介绍
        String introduction = req.getParameter("introduction");
        //身份证号
        String idNumber = req.getParameter("idNumber");

        /**放进Student对象类**/
        Student student = new Student();
        student.setStuNo(stuNo);
        student.setStuName(stuName);
        student.setEmail(email);
        student.setSex(Integer.parseInt(sex));
        student.setPhone(phone);
        student.setIdNumber(idNumber);
        student.setRegistered(registered);
        student.setAddRess(addRess);
        student.setProfession(profession);
        student.setPolitics(politics);
        student.setIntroduction(introduction);
        student.setGid(Integer.parseInt(gid));

        //调用serclet层
        StudentServiceImpl studentService = new StudentServiceImpl();
        int i = studentService.addStudent(student);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if(i>0){
            writer.println("<script>alert('添加成功');location.href='/Educational/student/Student?method=get';</script>");
        }else{
            writer.println("<script>alert('添加失败');location.href='/Educational/student/Student?method=get';</script>");
        }

    }

    //删除学生信息
    private void deleteStudent(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("utf-8");
        String sid = req.getParameter("sid");
        StudentServiceImpl studentService = new StudentServiceImpl();
        int delete = studentService.delete(sid);
        PrintWriter writer = resp.getWriter();
        if(delete>0){
            writer.println("<script>alert('删除成功');location.href='/Educational/student/Student?method=get';</script>");
        }else{
            writer.println("<script>alert('删除失败');location.href='/Educational/student/Student?method=get';</script>");
        }

    }

    //修改学生信息
    private void updateStudent(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String ugid = req.getParameter("usid");
        /**获取数据*/
        //姓名
        String stuName = req.getParameter("stuName");
        //学号
        String stuNo = req.getParameter("stuNo");
        //班级
        String gid = req.getParameter("gid");
        //性别
        String sex = req.getParameter("sex");
        //手机号
        String phone = req.getParameter("phone");
        //邮箱
        String email = req.getParameter("email");
        //户口所在地
        String registered = req.getParameter("registered");
        //住址
        String addRess = req.getParameter("addRess");
        //专业
        String profession = req.getParameter("profession");
        //政治面貌
        String politics = req.getParameter("politics");
        //介绍
        String introduction = req.getParameter("introduction");
        //身份证号
        String idNumber = req.getParameter("idNumber");

        /**放进Student对象类**/
        Student student = new Student();
        student.setStuNo(stuNo);
        student.setStuName(stuName);
        student.setEmail(email);
        student.setSex(Integer.parseInt(sex));
        student.setPhone(phone);
        student.setIdNumber(idNumber);
        student.setRegistered(registered);
        student.setAddRess(addRess);
        student.setProfession(profession);
        student.setPolitics(politics);
        student.setIntroduction(introduction);
        student.setGid(Integer.parseInt(gid));

        StudentServiceImpl studentService = new StudentServiceImpl();
        int i = studentService.upDate(ugid, student);
        PrintWriter writer = resp.getWriter();
        if(i>0){
            writer.println("<script>alert('修改成功');location.href='/Educational/student/Student?method=get';</script>");
        }else {
            writer.println("<script>alert('修改失败');location.href='/Educational/student/Student?method=get';</script>");

        }


    }

    //查找id
    private void findById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String gid = req.getParameter("sid");
        req.setAttribute("gsid",gid);
        StudentServiceImpl studentService = new StudentServiceImpl();
        GradeServiceImpl gradeService = new GradeServiceImpl();
        List<Grade> grades = gradeService.gatGread();
        req.setAttribute("ugrades",grades);
        Student byId = studentService.getById(gid);
        req.setAttribute("ustuno",byId.getStuNo());
        req.setAttribute("ustuname",byId.getStuName());
        req.setAttribute("usex",byId.getSex());
        req.setAttribute("uemail",byId.getEmail());
        req.setAttribute("uphone",byId.getPhone());
        req.setAttribute("uregistered",byId.getRegistered());
        req.setAttribute("uaddress",byId.getAddRess());
        req.setAttribute("upolitics",byId.getPolitics());
        req.setAttribute("uidnumber",byId.getIdNumber());
        req.setAttribute("uprofession",byId.getProfession());
        req.setAttribute("uintroduction",byId.getIntroduction());
        req.setAttribute("ugid",byId.getGid());
        req.getRequestDispatcher("edit.jsp").forward(req,resp);
    }

    //查看全部学生信息
    private void getStudent(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        //模糊查询的条件
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("utf-8");
        String stuName = req.getParameter("stuName");
        System.out.println(stuName);
        String stuId = req.getParameter("stuId");
        String sex = req.getParameter("sex");

        //分页查询   limit  开始位置  显示条数
        //页码值
        String pageIndex = req.getParameter("pageIndex");
        //如果页面没有传入pageindex值的话那就默认为第一页
        int index = pageIndex==null?1:Integer.parseInt(pageIndex);


        //调取service方法
        StudentServiceImpl studentService = new StudentServiceImpl();
        List<Student> student =
                studentService.getStudent(stuName,stuId,sex == null?-1:Integer.parseInt(sex),index,5);

        //总条数
        int total = studentService.total(stuName, stuId, sex == null ? -1 : Integer.parseInt(sex));

        //获取总页数=总条数%每页显示的条数>0?总条数/每页显示数+1:总条数/每页显示数
        int totalPages = total%5>0?total/5+1:total/5;
        req.setAttribute("stuname",stuName);
        req.setAttribute("id",stuId);
        req.setAttribute("sex",sex);
        req.setAttribute("stu",student);



        req.setAttribute("index",index);
        req.setAttribute("size",5);
        req.setAttribute("total",total);
        req.setAttribute("totalPages",totalPages);
        req.getRequestDispatcher("list.jsp").forward(req,resp);
    }
}
